<script language="javascript" type="text/javascript" src="js/CLEditor1_2/jquery.cleditor.min.js"></script>  

<link href="js/CLEditor1_2/jquery.cleditor.css" rel="stylesheet" type="text/css" /> 
<script type="text/javascript">
$(document).ready(function(){

			// Validate form
			$("#laytin").validate({
				errorElement: "span", // Định dạng cho thẻ HTML hiện thông báo lỗi
				rules: {
					
					
					
					chuyenmuc: { min: 1 }, //Giá trị tối thiểu
				},
				
				submitHandler: function(form) {
					$('#loading').html("<img src='images/loading.gif'/>").fadeIn(500);
					var tieude = $("input#tieude").val();
					var tomtat = $("textarea#tomtat").val();
					var noidung = $("textarea#noidung").val();
					var urlHinh = $("input#urlHinh").val();
					var diachi = $("input#diachi").val();
					var chuyenmuc = $("select#chuyenmuc").val();
					var trangthai = $("input#trangthai").val();
					var tinnoibat = $("input#tinnoibat").val();
					$.post(window.location.href+"/add",{tieude:tieude,tomtat:tomtat,noidung:noidung,diachi:diachi,urlHinh:urlHinh,chuyenmuc:chuyenmuc,trangthai:trangthai,tinnoibat:tinnoibat},function(data){
						$('#loading').fadeOut(500);
						$('#error').hide();
							$('#error').slideToggle(100); // hiển thị thẻ div success
							$('#error').html(data); // đặt kết quả trả về từ test.php vào thẻ div success
					})
					return false;
				}
			});
	
});
</script>
<script type="text/javascript">
$(document).ready(function(){
				 $("#noidung").cleditor({width:800,height:400});//khởi động WYSIWYG Editor
        		var editor = $("#noidung").cleditor()[0]; //khai báo biến chứa editor này
			
				$("select#lienket").change(function() {
					$('#loading').html("<img src='images/loading.gif'/>").fadeIn(500);
						if(this.href==""){
							$("input#tieude").val("");
							$("textarea#tomtat").val("");
							$("textarea#noidung").val("");							
							$("input#diachi").val("");
							$("div#content").html("");
							editor.updateFrame();
							return;
						}
						
						$.post("#", {url:this.value}, function(data){
							if(data){
								var item = data.split("---+++---+++");
								$("input#tieude").val(item[0]);
								$("textarea#tomtat").val(item[1]);
								$("textarea#noidung").val(item[2]);
								editor.updateFrame();
								$("input#diachi").val(item[3]);
								//$("div#content").html("<h3>"+item[0]+"</h3>"+"<h4>"+item[1]+"</h4>"+item[2]);
								
								var hinh=item[2].split("src=\"");
                                var x = hinh[1].split(">");
                                var mrk_len = x[0].length-1; // loai bo " cuoiimg
                                var mrk_hinh = x[0].substr(0, mrk_len );
                                $("input#urlHinh").val(mrk_hinh);
								$("#mrk").attr("src", mrk_hinh);
								
								

								}else{
									this.value="";
								}
								$('#loading').fadeOut(500);
						})
						
						
				});
		
				
				$("input#urlHinh").change(function(){
					var img = $("input#urlHinh").val();
					$("#mrk").attr("src", img);
					})
		
    });
</script> 

<?php
$title = 'Thêm bài viết';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>

<?php


$form = $this->form;
$form->setAttribute('action', $this->url('laytin', array('action' => 'index')));
$form->prepare();

echo $this->form()->openTag($form) . PHP_EOL;
echo $this->formHidden($form->get('id')) . PHP_EOL;
//echo $this->formRow($form->get('lienket')) . PHP_EOL;
?>
<select id="lienket" name="lienket" required="required"> 
	<option selected="selected" value="">Xin chọn liên kết</option> 
    <?php echo $this->label; ?>
</select>
<?php
echo $this->formRow($form->get('diachi')) . PHP_EOL;
echo $this->formRow($form->get('tieude')) . PHP_EOL;
echo $this->formRow($form->get('tomtat')) . PHP_EOL;
echo '<br><img src="images/img-blank.png" width="150" height="114" id="mrk" />';
echo $this->formRow($form->get('urlHinh')) . PHP_EOL;
echo '<font color="#FF0033"> Nếu hình ảnh không hiển thị được, hãy thử sửa lại đường dẫn</font>';
echo $this->formRow($form->get('noidung')) . PHP_EOL;


echo $this->formRow($form->get('chuyenmuc')) . PHP_EOL;
echo "<label>Trạng thái:</label>";
echo $this->formRow($form->get('trangthai')) . PHP_EOL;
echo "<label>Tin nổi bật:</label>";
echo $this->formRow($form->get('tinnoibat')) . PHP_EOL;
echo "<br>".$this->formInput($form->get('submit')) . PHP_EOL;
echo $this->form()->closeTag($form) . PHP_EOL;


?>
<div id="error" class="error"></div>
<div id="loading"></div>